const { Success, Fail, Custom } = require("../../utils/status");
const { sign, verify } = require("../../utils/jwt");
const { getUser, getAlluser, updateUserTime, addUserOne } = require("../../service/user")
const moment = require("moment");
module.exports = {
    login: async function (ctx, next) {
        const { body } = ctx.request;
        const { username, password } = body;
        var send;
        if (username && password) {
            var user = await getUser(username);
            if (user && user.password === password) {
                await updateUserTime({
                    oldTime: moment().format("YYYY-MM-DD HH:mm:ss"),
                    id: user.id
                })
                send = new Success({
                    data: {
                        token: sign({
                            username: user.username,
                            id: user.id
                        })
                    },
                    msg: "登录成功"
                })
            } else {
                send = new Fail({
                    msg: "账号或者密码错误！"
                })
            }
        } else {
            send = new Fail({
                msg: "参数错误！"
            })
        }
        ctx.body = send
    },
    roles: async function (ctx, next) {
        const { authorization = "" } = ctx.header;
        var send;
        if (authorization) {
            var user = verify(authorization);
            if (user) {
                var isUser = await getUser(user.username);
                if (isUser) {
                    send = new Success({
                        data: user.username,
                        msg: "成功！"
                    })
                } else {
                    send = new Fail({
                        msg: "token无效！"
                    })
                }
            } else {
                send = new Fail({
                    msg: "token失效！"
                })
            }
        } else {
            send = new Fail({
                msg: "参数错误！"
            })
        }
        ctx.body = send
    },
    userList: async function (ctx, next) {
        let { id, username } = ctx.state.user;
        let { pageSize = 10, pageNum = 1, username: name } = ctx.request.body;
        var send;
        if (id == 1 && username == "admin") {
            var { count, rows } = await getAlluser({ pageSize, pageNum, username: name });
            send = new Success({
                data: {
                    data: rows,
                    total: count
                },
                msg: "成功!"
            })
        } else {
            send = new Fail({
                msg: "您权限不够嗷！"
            })
        }
        ctx.body = send
    },
    addUser: async function (ctx, next) {
        let { id, username } = ctx.state.user;
        let { username: name, password, surePassword } = ctx.request.body;
        var send;
        if (id == 1 && username == "admin") {
            if (name && password && surePassword && surePassword == password) {
                var data = await getUser(name);
                if (!data) {
                    await addUserOne({
                        password,
                        username: name,
                        createTime: moment().format("YYYY-MM-DD HH:mm:ss")
                    });
                    send = new Success({
                        msg: "成功!"
                    })
                } else {
                    send = new Fail({
                        msg: "用户名不能重复！"
                    })
                }
            } else {
                send = new Fail({
                    msg: "参数错误！"
                })
            }
        } else {
            send = new Fail({
                msg: "您权限不够嗷！"
            })
        }
        ctx.body = send
    },

}